<?php


namespace app\bus\controller;


use app\admin\controller\Intermediary;
use think\Controller;
use think\Db;

class Login extends Controller
{
    public function login()
    {
        //访问页面
        if ($this->request->isGet()) {
            return view();
        }
        else{
            $password = input('password');
            $account = input('account');
            
            if(empty($account)) return errorMsg('账号不能为空!');
            if(empty($password)) return errorMsg('密码不能为空!');

            $info = DB::name('l_user')
                ->where(['account'=>$account,'password'=>$password,'status'=>0,'is_delete'=>0])
                ->find();

            if(empty($info)) return errorMsg('账号密码不匹配,请重试!');

            session('INDEX_USER_INFO',$info);
            return success($info);

        }
    }

    public function getMessage($code)
    {
        $appId = config('wxLoginAppId');
        $appSecret = config('wxLoginAppSecret');

        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$appSecret.'&code='.$code.'&grant_type=authorization_code';
        $wxInfo = $this->curlGet($url);
        $token = $wxInfo['access_token'];
        $openId = $wxInfo['openid'];
        $userUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openId;
        $userInfo = $this->curlGet($userUrl);
        return $userInfo;
    }


    public function bind()
    {

        $openid = session('openId');
        $this->assign('openid',$openid);
        return view();
    }

    public function verificationAccount()
    {
        $data = input('data/a');
        $result = DB::Table('s_user')
                ->where(['account'=>$data['name'],'password'=>$data['password']])
                ->where(['is_delete'=>0,'status'=>'正常'])
                ->find();
        //验证账号密码
        if(empty($result))  return json(['code'=>-1,'msg'=>'当前账号密码不匹配,请重试']);
        //如果是中介,则验证服务有效期
        if(!empty($result['intermediary_id']))
        {
            $intermedFunc = new Intermediary();
            $info = $intermedFunc->content->table($intermedFunc->database)
                          ->where(['id'=>$result['intermediary_id']])
                          ->field('validity_date,is_delete')
                            ->find();
            $validityDate = $info['validity_date'];
            if($info['is_delete'] == 1) return json(['code'=>-1,'msg'=>'当前服务已到期,请及时联系普腾相关人员']);
            if(!empty($validityDate) && strtotime($validityDate) < strtotime(date('Y-m-d')))
            {
               return json(['code'=>-1,'msg'=>'当前服务已到期,请及时联系普腾相关人员']);
            }
        }
        if(!empty($result['wxOpenId'])) return json(['code'=>-1,'msg'=>'当前账号已绑定,请重试']);
        //更新openid
        DB::table('s_user')
            ->where(['id'=>$result['id']])
            ->update(['wxOpenId'=>$data['openid'],"last_login_date"=>get_date_time()]);


        session('MAP_USER', $result["id"]);

        wlog("用户登陆","微信扫码登录成功，用户名:".$result['account']);


        return json(['code'=>0,'msg'=>'登录成功,正在进入系统.....']);

    }


    public function curlGet($url){
        if(empty($url)){
            return false;
        }
        $output = '';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }


}